package com.example.myapplication;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class DevicePowerInfoActivity extends AppCompatActivity {
    private DevicePowerInfoService powerInfoService;
    private Handler handler;
    private Runnable updateRunnable;

    // UI组件
    private TextView textSystemUptime;
    private TextView textBootTime;
    private TextView textCurrentTime;
    private TextView textDeviceModel;
    private TextView textAndroidVersion;
    private TextView textApiLevel;
    private TextView textBatteryLevel;
    private TextView textChargingStatus;
    private TextView textScreenStatus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_power_info);

        // 初始化服务
        powerInfoService = new DevicePowerInfoService(this);

        // 初始化UI组件
        initViews();

        // 开始实时更新
        startRealTimeUpdate();
    }

    private void initViews() {
        textSystemUptime = findViewById(R.id.text_system_uptime);
        textBootTime = findViewById(R.id.text_boot_time);
        textCurrentTime = findViewById(R.id.text_current_time);
        textDeviceModel = findViewById(R.id.text_device_model);
        textAndroidVersion = findViewById(R.id.text_android_version);
        textApiLevel = findViewById(R.id.text_api_level);
        textBatteryLevel = findViewById(R.id.text_battery_level);
        textChargingStatus = findViewById(R.id.text_charging_status);
        textScreenStatus = findViewById(R.id.text_screen_status);
    }

    private void startRealTimeUpdate() {
        handler = new Handler(Looper.getMainLooper());
        updateRunnable = new Runnable() {
            @Override
            public void run() {
                updatePowerInfo();
                // 每秒更新一次
                handler.postDelayed(this, 1000);
            }
        };
        handler.post(updateRunnable);
    }

    private void updatePowerInfo() {
        DevicePowerInfoService.DevicePowerInfo info = powerInfoService.getDevicePowerInfo();

        // 更新开机时长信息
        textSystemUptime.setText("系统开机累计时长: " + info.systemUptimeFormatted);
        textBootTime.setText("开机时间: " + info.bootTimeFormatted);
        textCurrentTime.setText("当前时间: " + info.currentTimeFormatted);

        // 更新设备信息
        textDeviceModel.setText("设备型号: " + info.deviceModel);
        textAndroidVersion.setText("Android版本: " + info.androidVersion);
        textApiLevel.setText("API级别: " + info.apiLevel);

        // 更新电池信息
        if (info.batteryLevel >= 0) {
            textBatteryLevel.setText("电池电量: " + info.batteryLevel + "%");
        } else {
            textBatteryLevel.setText("电池电量: 无法获取");
        }

        if (info.isCharging != null) {
            textChargingStatus.setText("充电状态: " + (info.isCharging ? "正在充电" : "未充电"));
        } else {
            textChargingStatus.setText("充电状态: 无法确定");
        }

        // 更新屏幕状态
        textScreenStatus.setText("屏幕状态: " + (info.isScreenOn ? "开启" : "关闭"));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (handler != null && updateRunnable != null) {
            handler.removeCallbacks(updateRunnable);
        }
    }
}
